﻿
Partial Class TaoBao_Mode
    Inherits System.Web.UI.Page
    Public Shared Function MD5(ByVal strSource As String, ByVal Code As Int16) As String
        '这里用的是ascii编码密码原文，如果要用汉字做密码，可以用UnicodeEncoding，但会与ASP中的MD5函数不兼容 
        Dim dataToHash As Byte() = (New System.Text.ASCIIEncoding).GetBytes(strSource)
        Dim hashvalue As Byte() = CType(System.Security.Cryptography.CryptoConfig.CreateFromName("MD5"), System.Security.Cryptography.HashAlgorithm).ComputeHash(dataToHash)
        Dim i As Integer
        Select Case Code
            Case 16 '´选择16位字符的加密结果 
                For i = 4 To 11
                    MD5 += Hex(hashvalue(i)).ToLower
                Next
            Case 32 ' ´选择32位字符的加密结果 
                For i = 0 To 15
                    MD5 += Hex(hashvalue(i)).ToLower
                Next
            Case Else ' ´Code错误时，返回全部字符串，即32位字符 
                For i = 0 To hashvalue.Length - 1
                    MD5 += Hex(hashvalue(i)).ToLower
                Next
        End Select
    End Function
    Function EncodeBase64(ByVal StrA)
        Dim BufferA As Byte()
        BufferA = System.Text.Encoding.Default.GetBytes(StrA)
        Dim StrB As String
        StrB = Convert.ToBase64String(BufferA)
        EncodeBase64 = StrB
    End Function
    Function DecodeBase64(ByVal StrA)
        DecodeBase64 = Encoding.GetEncoding("gb2312").GetString(Convert.FromBase64String(StrA))
    End Function
    Function getstr(ByVal iRemote)
        On Error Resume Next
        Dim xPost, sGet
        iRemote = iRemote
        xPost = SERVER.CreateObject("msxml2.serverxmlhttp") 'msxml2.serverxmlhttp
        xPost.Open("GET", iRemote, False)
        xPost.setrequestheader("referer", "http://taoke.alimama.com/spreader/search_auction.htm")
        xPost.Send()
        If err.number <> 0 Then
            response.write("GetRemoteModeList|error")
            response.end()
        End If
        sGet = CreateObject("ADODB.Stream")
        sGet.Mode = 3
        sGet.Type = 1
        sGet.Open()


        sGet.Write(xPost.responseBody)
        sGet.Position = 0
        sGet.Type = 2
        sGet.Charset = "gb2312" ' "gb2312"
        getstr = sGet.ReadText
    End Function


    Function LoadFile(ByVal File)
        Dim objStream
        On Error Resume Next
        objStream = Server.CreateObject("ADODB.Stream")
        If Err.Number = -2147221005 Then
            Err.Clear()
            Response.End()
        End If
        With objStream
            .Type = 2
            .Mode = 3
            .Open()
            .LoadFromFile(Server.MapPath(File))
            If Err.Number <> 0 Then
                Err.Clear()
                Response.End()
            End If
            .Charset = "GB2312"
            .Position = 2
            LoadFile = .ReadText
            .Close()
        End With
        objStream = Nothing
    End Function



    Public Shared Function MD5x(ByVal strSource As String, ByVal Code As Int16) As String
        '这里用的是ascii编码密码原文，如果要用汉字做密码，可以用UnicodeEncoding，但会与ASP中的MD5函数不兼容 
        Select Case Code
            Case 16 '´选择16位字符的加密结果 
                MD5x = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strSource, "MD5").ToLower().Substring(8, 16)
            Case 32 ' ´选择32位字符的加密结果 
                MD5x = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strSource, "MD5").ToLower()
        End Select
    End Function
    Function WriteFile(ByVal f, ByVal Txt)
        Dim Fso, ts
        'On Error Resume Next
        Fso = Server.CreateObject("Scripting.FileSystemObject")
        If (Fso.FolderExists(Server.MapPath("../ModeHtml"))) Then '判断文件夹是否存在
        Else
            Fso.CreateFolder(Server.MapPath("../ModeHtml"))
        End If
        'response.write(Server.MapPath(f))
        'response.end()
        ts = Fso.createtextfile(Server.MapPath(f), True)
        ts.write(Txt)
        ts.close()
    End Function
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim connstr, connA, rsA
        connstr = ConfigurationSettings.AppSettings("SQLConnString") & """" & Server.MapPath(".") & "\..\" & ConfigurationSettings.AppSettings("dbPath") & """"
        connA = Server.CreateObject("ADODB.Connection")
        connA.open(connstr)

        If Request.Cookies("TaoddAdmin") Is Nothing Or Request.Cookies("TaoddPassWord") Is Nothing Then
            Response.Redirect("login.aspx")
        Else
            Dim Admin_UserName, Admin_UserPass
            Admin_UserName = replace(Request.Cookies("TaoddAdmin").value, "'", "''")
            Admin_UserPass = replace(Request.Cookies("TaoddPassWord").value, "'", "''")
            rsA = Server.CreateObject("adodb.recordset")
            rsA.open("select * from [admin^] where Admin_UserName='" & Admin_UserName & "' and Admin_UserPass='" & Admin_UserPass & "'", connA, 1, 3)
            If rsA.eof Then
                Response.Redirect("login.aspx")
            End If
            rsA.close()
        End If
        If request("Action") = "DownLoadModeFile" Then
            On Error Resume Next
            Dim url, modecode
            rsA.open("select  top 1 *  from [TaobaoMode] where do=false", connA, 1, 3)
            If Not rsA.eof Then
                url = "http://support.taodd.org/" & rsA("url").value
                modecode = ""
                modecode = getstr(url)
                WriteFile("../" & rsA("url").value, modecode)
                response.write("DownLoadModeFile|ok")
                rsA("do") = True
                rsA.update()
            Else
                response.write("DownLoadModeFile|finish")
            End If
            rsA.close()
            response.end()

        End If
        If request("Action") = "GetRemoteModeList" Then
            Dim code, itemarr, i, curarr, k
            code = getstr("http://support.taodd.org/modefilelist.asp")
            If instr(code, "|split|") Then
                itemarr = split(code, "|split|")
                k = 0
                For i = 0 To ubound(itemarr)
                    If itemarr(i) <> "" Then
                        curarr = split(itemarr(i), "_|_")
                        rsA.open("select  *  from [TaobaoMode] where url='" & curarr(0) & "'", connA, 1, 3)
                        If rsA.eof Then
                            k = k + 1
                            rsA.addnew()
                            rsA("url").value = curarr(0)
                            If isnumeric(curarr(1)) Then rsA("width").value = curarr(1)
                            If isnumeric(curarr(2)) Then rsA("height").value = curarr(2)
                            If isnumeric(curarr(3)) Then rsA("type").value = curarr(3)
                            If curarr(4) = "True" Then rsA("c1").value = True
                            If curarr(5) = "True" Then rsA("c3").value = True
                            If curarr(6) = "True" Then rsA("c5").value = True
                            If curarr(7) = "True" Then rsA("c6").value = True
                            If curarr(8) = "True" Then rsA("c7").value = True
                            If curarr(9) = "True" Then rsA("c8").value = True
                            If curarr(10) = "True" Then rsA("c9").value = True
                            If curarr(11) = "True" Then rsA("c10").value = True
                            rsA.update()
                        End If
                        rsA.close()
                    End If
                Next
                rsA.open("select  *  from [TaobaoMode] where do=false", connA, 1, 3)

                response.write("GetRemoteModeList|" & rsA.recordcount)
                rsA.close()
                response.end()
            End If


        End If
    End Sub
    Function PassPortCheck()


    End Function
End Class
